package com.yc.springmvc.web;

import com.yc.springmvc.bean.Hall;
import com.yc.springmvc.bean.Movie;
import com.yc.springmvc.bean.Plan;
import com.yc.springmvc.bean.Result;
import lombok.Data;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import sun.font.EAttribute;

import javax.print.attribute.standard.Media;
import javax.print.attribute.standard.MediaTray;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.LinkedHashSet;
import java.util.Set;


/**
 * Controller :标注的类，内部方法的返回值默认为视图名
 * jsp ，thymeleaf，。。。
 * <p>
 * RestController  :   .......    默认将返回值转成JSon格式数据 ， 适用于前后端分离应用
 */


@RestController
@RequestMapping("web/page")
public class HelloAction {
    @RequestMapping("hello.do")
    public String hello() {
        return "Hello world";
    }

    //接口注入
    //ModelAndView  表示该方法,将会跳转到指定页面(视图)
    @RequestMapping("tohome.do")
    ModelAndView toHome(ModelAndView modelAndView) {
        //响应重定向跳转到home
        modelAndView.setViewName("redirect:/home.html");
        return modelAndView;
    }

    @RequestMapping(path = "get.do", method = RequestMethod.GET)
    Result get() {
        return new Result(1, "doGet", null);
    }

    @RequestMapping(path = "getOrpost.do",
            method = {RequestMethod.GET, RequestMethod.POST})
    Result getOrPost() {
        return new Result(1, "getOrPost", null);
    }

    @GetMapping(path = "login1", params = "email")
    Result loginWithEmail(String email) {
        return new Result(1, "loginWithEmail", email);
    }

    @GetMapping(path = "login1", params = "email=qq@qq.com")
    Result loginWithEmail1(String email) {
        return new Result(1, "loginWithEmail1", email);
    }

    @GetMapping(path = "header.do", headers = "Host=localhost")
    String header1() {
        return "localhost";
    }

    @GetMapping(path = "header.do", headers = "Host=127.0.0.1")
    String header2() {
        return "127.0.0.1";
    }

    @PostMapping(path = "consumes.do",
            //必须是表单提交请求
            consumes = MediaType.TEXT_PLAIN_VALUE)
    String consume() {
        return "consums" + MediaType.TEXT_PLAIN_VALUE;
    }

    @GetMapping(path = "produces1.do")
    String produce1() {
        return "produces 中文";
    }

    //Content-type
    @GetMapping(path = "produces2.du", produces = MediaType.TEXT_HTML_VALUE + ";charset=UTF-8")
    String produce2() {
        return "produces 中文";
    }

    //自动映射
    @GetMapping("login")
    String login2(String uname, String pwd , int vcode){
        return uname + "," + pwd + "," + vcode;
    }

    //自动装箱
    @GetMapping("hall")
    Hall hall(Hall abc){
        return abc;
    }

    @GetMapping("plan")
    Plan plan(Plan abc){
        return abc;
    }

    @RequestMapping("movie")
        Movie movie(Movie abc){
            return abc;
    }

    //@RequestMapping  将请求头中的数据(json)  装载到abc中
    @RequestMapping("movie2")
    Movie movie2(@RequestBody Movie abc){
        return abc;
    }

    //注入Servlet  相关对象
    @GetMapping("servletObjects.do")
    Set<Object> getServletObjects(
            HttpServletRequest request,
            HttpServletResponse response,
            HttpSession session,
            InputStream inputStream // 请求的输入流
            //OutputStream outputStream  //响应输出流
            //PrintWriter out
    ){
      Set<Object>  ret = new LinkedHashSet<>();
      ret.add(request);
      ret.add(response);
      ret.add(session);
      ret.add(inputStream);
      return ret;
    }
}

